package com.sunday.electric.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sunday.common.logger.Logger;
import com.sunday.common.utils.Constants;
import com.sunday.electric.BuildConfig;
import com.sunday.electric.R;
import com.sunday.electric.db.MessageDB;
import com.sunday.electric.db.RecentDB;
import com.sunday.electric.db.UserDB;
import com.sunday.electric.utils.SharePerferenceUtils;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * Created by Administrator on 2016/1/7.
 */
public class BaseApplication extends Application implements Thread.UncaughtExceptionHandler {
    private static BaseApplication instance;

    private String userId;

    public synchronized static BaseApplication getInstance() {
        return instance;
    }

    public static final int NUM_PAGE = 6;// 总共有多少页
    public static int NUM = 20;// 每页20个表情,还有最后一个删除button
    private Map<String, Integer> mFaceMap = new LinkedHashMap<>();

    private UserDB mUserDB;
    private MessageDB mMsgDB;
    private RecentDB mRecentDB;
    private Gson mGson;

    public LocationClient mLocationClient;
    public MyLocationListener mMyLocationListener;

    private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
    private String tempcoor = "bd09ll";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;


            /**
             * IMKit SDK调用第一步 初始化
             */
            // RongIM.init(this);

            mLocationClient = new LocationClient(this.getApplicationContext());
            mMyLocationListener = new MyLocationListener();
            mLocationClient.registerLocationListener(mMyLocationListener);
            initLocation();
            mLocationClient.start();
            Logger.init("Sunday").hideThreadInfo().setMethodCount(3).setMethodOffset(2);
        //Thread.setDefaultUncaughtExceptionHandler(this);
        initFaceMap();
        initData();
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(tempMode);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType(tempcoor);//可选，默认gcj02，设置返回的定位结果坐标系，
        int span = 1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        mLocationClient.setLocOption(option);
    }

    private void initData() {
        // 不转换没有 @Expose 注解的字段
        mGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                .create();
        mUserDB = new UserDB(this);
        mMsgDB = new MessageDB(this);
        mRecentDB = new RecentDB(this);
        // mUserList = mUserDB.getUser();
        // mMediaPlayer = MediaPlayer.create(this, R.raw.office);
        //  mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    }

    public String getUserId() {
        if (TextUtils.isEmpty(userId)) {
            userId = SharePerferenceUtils.getIns().getUserId();
        }

        return userId;
    }

    public synchronized UserDB getUserDB() {
        if (mUserDB == null)
            mUserDB = new UserDB(this);
        return mUserDB;
    }

    public synchronized Gson getGson() {
        if (mGson == null)
            // 不转换没有 @Expose 注解的字段
            mGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                    .create();
        return mGson;
    }

    public synchronized MessageDB getMessageDB() {
        if (mMsgDB == null)
            mMsgDB = new MessageDB(this);
        return mMsgDB;
    }

    public synchronized RecentDB getRecentDB() {
        if (mRecentDB == null)
            mRecentDB = new RecentDB(this);
        return mRecentDB;
    }


    public Map<String, Integer> getFaceMap() {
        if (!mFaceMap.isEmpty())
            return mFaceMap;
        return null;
    }

    private void initFaceMap() {
        // TODO Auto-generated method stub
        mFaceMap.put("[呲牙]", R.drawable.f000);
        mFaceMap.put("[调皮]", R.drawable.f001);
        mFaceMap.put("[流汗]", R.drawable.f002);
        mFaceMap.put("[偷笑]", R.drawable.f003);
        mFaceMap.put("[再见]", R.drawable.f004);
        mFaceMap.put("[敲打]", R.drawable.f005);
        mFaceMap.put("[擦汗]", R.drawable.f006);
        mFaceMap.put("[猪头]", R.drawable.f007);
        mFaceMap.put("[玫瑰]", R.drawable.f008);
        mFaceMap.put("[流泪]", R.drawable.f009);
        mFaceMap.put("[大哭]", R.drawable.f010);
        mFaceMap.put("[嘘]", R.drawable.f011);
        mFaceMap.put("[酷]", R.drawable.f012);
        mFaceMap.put("[抓狂]", R.drawable.f013);
        mFaceMap.put("[委屈]", R.drawable.f014);
        mFaceMap.put("[便便]", R.drawable.f015);
        mFaceMap.put("[炸弹]", R.drawable.f016);
        mFaceMap.put("[菜刀]", R.drawable.f017);
        mFaceMap.put("[可爱]", R.drawable.f018);
        mFaceMap.put("[色]", R.drawable.f019);
        mFaceMap.put("[害羞]", R.drawable.f020);

        mFaceMap.put("[得意]", R.drawable.f021);
        mFaceMap.put("[吐]", R.drawable.f022);
        mFaceMap.put("[微笑]", R.drawable.f023);
        mFaceMap.put("[发怒]", R.drawable.f024);
        mFaceMap.put("[尴尬]", R.drawable.f025);
        mFaceMap.put("[惊恐]", R.drawable.f026);
        mFaceMap.put("[冷汗]", R.drawable.f027);
        mFaceMap.put("[爱心]", R.drawable.f028);
        mFaceMap.put("[示爱]", R.drawable.f029);
        mFaceMap.put("[白眼]", R.drawable.f030);
        mFaceMap.put("[傲慢]", R.drawable.f031);
        mFaceMap.put("[难过]", R.drawable.f032);
        mFaceMap.put("[惊讶]", R.drawable.f033);
        mFaceMap.put("[疑问]", R.drawable.f034);
        mFaceMap.put("[睡]", R.drawable.f035);
        mFaceMap.put("[亲亲]", R.drawable.f036);
        mFaceMap.put("[憨笑]", R.drawable.f037);
        mFaceMap.put("[爱情]", R.drawable.f038);
        mFaceMap.put("[衰]", R.drawable.f039);
        mFaceMap.put("[撇嘴]", R.drawable.f040);
        mFaceMap.put("[阴险]", R.drawable.f041);

        mFaceMap.put("[奋斗]", R.drawable.f042);
        mFaceMap.put("[发呆]", R.drawable.f043);
        mFaceMap.put("[右哼哼]", R.drawable.f044);
        mFaceMap.put("[拥抱]", R.drawable.f045);
        mFaceMap.put("[坏笑]", R.drawable.f046);
        mFaceMap.put("[飞吻]", R.drawable.f047);
        mFaceMap.put("[鄙视]", R.drawable.f048);
        mFaceMap.put("[晕]", R.drawable.f049);
        mFaceMap.put("[大兵]", R.drawable.f050);
        mFaceMap.put("[可怜]", R.drawable.f051);
        mFaceMap.put("[强]", R.drawable.f052);
        mFaceMap.put("[弱]", R.drawable.f053);
        mFaceMap.put("[握手]", R.drawable.f054);
        mFaceMap.put("[胜利]", R.drawable.f055);
        mFaceMap.put("[抱拳]", R.drawable.f056);
        mFaceMap.put("[凋谢]", R.drawable.f057);
        mFaceMap.put("[饭]", R.drawable.f058);
        mFaceMap.put("[蛋糕]", R.drawable.f059);
        mFaceMap.put("[西瓜]", R.drawable.f060);
        mFaceMap.put("[啤酒]", R.drawable.f061);
        mFaceMap.put("[飘虫]", R.drawable.f062);

        mFaceMap.put("[勾引]", R.drawable.f063);
        mFaceMap.put("[OK]", R.drawable.f064);
        mFaceMap.put("[爱你]", R.drawable.f065);
        mFaceMap.put("[咖啡]", R.drawable.f066);
        mFaceMap.put("[钱]", R.drawable.f067);
        mFaceMap.put("[月亮]", R.drawable.f068);
        mFaceMap.put("[美女]", R.drawable.f069);
        mFaceMap.put("[刀]", R.drawable.f070);
        mFaceMap.put("[发抖]", R.drawable.f071);
        mFaceMap.put("[差劲]", R.drawable.f072);
        mFaceMap.put("[拳头]", R.drawable.f073);
        mFaceMap.put("[心碎]", R.drawable.f074);
        mFaceMap.put("[太阳]", R.drawable.f075);
        mFaceMap.put("[礼物]", R.drawable.f076);
        mFaceMap.put("[足球]", R.drawable.f077);
        mFaceMap.put("[骷髅]", R.drawable.f078);
        mFaceMap.put("[挥手]", R.drawable.f079);
        mFaceMap.put("[闪电]", R.drawable.f080);
        mFaceMap.put("[饥饿]", R.drawable.f081);
        mFaceMap.put("[困]", R.drawable.f082);
        mFaceMap.put("[咒骂]", R.drawable.f083);

        mFaceMap.put("[折磨]", R.drawable.f084);
        mFaceMap.put("[抠鼻]", R.drawable.f085);
        mFaceMap.put("[鼓掌]", R.drawable.f086);
        mFaceMap.put("[糗大了]", R.drawable.f087);
        mFaceMap.put("[左哼哼]", R.drawable.f088);
        mFaceMap.put("[哈欠]", R.drawable.f089);
        mFaceMap.put("[快哭了]", R.drawable.f090);
        mFaceMap.put("[吓]", R.drawable.f091);
        mFaceMap.put("[篮球]", R.drawable.f092);
        mFaceMap.put("[乒乓球]", R.drawable.f093);
        mFaceMap.put("[NO]", R.drawable.f094);
        mFaceMap.put("[跳跳]", R.drawable.f095);
        mFaceMap.put("[怄火]", R.drawable.f096);
        mFaceMap.put("[转圈]", R.drawable.f097);
        mFaceMap.put("[磕头]", R.drawable.f098);
        mFaceMap.put("[回头]", R.drawable.f099);
        mFaceMap.put("[跳绳]", R.drawable.f100);
        mFaceMap.put("[激动]", R.drawable.f101);
        mFaceMap.put("[街舞]", R.drawable.f102);
        mFaceMap.put("[献吻]", R.drawable.f103);
        mFaceMap.put("[左太极]", R.drawable.f104);
        mFaceMap.put("[右太极]", R.drawable.f105);
        mFaceMap.put("[闭嘴]", R.drawable.f106);
    }


    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return 进程号
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }


/**
 * 实现实时位置回调监听
 */
public class MyLocationListener implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location.getLatitude() != 0) {
            SharePerferenceUtils.getIns().putString(Constants.LATITUDE, String.valueOf(location.getLatitude()));
        }
        if (location.getLongitude() != 0) {
            SharePerferenceUtils.getIns().putString(Constants.LONGITUDE, String.valueOf(location.getLongitude()));
        }
        if (!TextUtils.isEmpty(location.getAddrStr())) {
            SharePerferenceUtils.getIns().putString(Constants.LOCATION_ADDRESS, location.getAddrStr());
        }
        if (!TextUtils.isEmpty(location.getProvince())) {
            SharePerferenceUtils.getIns().putString(Constants.LOCATION_PROVINCE, location.getProvince());
        }
        if (!TextUtils.isEmpty(location.getCity())) {
            SharePerferenceUtils.getIns().putString(Constants.LOCALTION_CITY, String.valueOf(location.getCity()));
        }
        if (!TextUtils.isEmpty(location.getDistrict())) {
            SharePerferenceUtils.getIns().putString(Constants.LOCALTION_DISTRCT, String.valueOf(location.getDistrict()));
        }
        mLocationClient.stop();
    }


}


    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        if (!BuildConfig.DEBUG) {
            System.exit(1);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
}
